package spark.ml

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.ml.feature.{Word2Vec, Word2VecModel}
import org.apache.spark.sql.SQLContext
/**
  * Created by root on 16-6-7.
  */
object Word2VecDemo {

  def saveModel(): Unit ={
    val sparkConf = new SparkConf().setAppName("word2vecDemo").setMaster("local")
    val sc = new SparkContext(sparkConf)
    val sqlContext = new SQLContext(sc)

    val documentDF = sqlContext.createDataFrame(Seq(
      "Hi I heard about Spark".split(" "),
      "I wish Java could use case classes".split(" "),
      "Logistic regression models are neat".split(" ")
    ).map(Tuple1.apply)).toDF("text")

    // Learn a mapping from words to Vectors.
    val word2Vec = new Word2Vec()
      .setInputCol("text")
      .setOutputCol("result")
      .setVectorSize(3)
      .setMinCount(0)
    val model = word2Vec.fit(documentDF)
    val result = model.transform(documentDF)
    result.show(3)
    model.write.overwrite().save("w2v_model")
  }

  //ml load model的时候，不需要传入sparkContext作为参数，但是需要有sparkContext的环境才能运行
  def loadModel(): Unit ={
    val sparkConf = new SparkConf().setAppName("word2vecDemo").setMaster("local")
    new SparkContext(sparkConf)
    val model = Word2VecModel.load("w2v_model")
    model.findSynonyms("regression",3).show(3)
  }

  def main(args: Array[String]): Unit = {
    saveModel()
  }
}
